// 封装组合式api的模块  放置的是 组合式api的业务函数 封装是 hook函数 推荐以 useXX命名
import { OrderType } from '@/enums'
import {
  cancelOrder,
  delOrder,
  followOrUnfollow,
  getPreInfo
} from '@/services/consult'
import { getMedicalOrderDetail } from '@/services/order'
import { sendCodeToMobile } from '@/services/user'
import type { ConsultOrderItem, FollowType } from '@/types/consult'
import type { OrderDetail } from '@/types/order'
import type { SendCodeType } from '@/types/user'
import {
  showImagePreview,
  showSuccessToast,
  showToast,
  type FormInstance
} from 'vant'
import { onUnmounted, type Ref } from 'vue'
import { ref } from 'vue'
// 关注和取消关注的hook函数
export const useFollow = (type: FollowType) => {
  const loading = ref(false)
  const follow = async (item: { id: string; likeFlag: number }) => {
    loading.value = true
    // 1. 发送关注/取消关注的请求
    await followOrUnfollow({ type: type, id: item.id })
    // 2. 提示
    item.likeFlag ? showToast('取消关注成功') : showToast('关注成功')
    // 3. 更新视图
    item.likeFlag = item.likeFlag ? 0 : 1
    // 4. 关闭loading
    loading.value = false
  }
  return { loading, follow }
}

// 预览处方的hook函数
export const useShowPreInfo = () => {
  // 预览处方单
  const showPerInfo = async (id: string) => {
    if (!id) return showToast('暂无处方信息')
    const res = await getPreInfo(id)
    if (!res.data.url) return showToast('暂无处方信息')
    showImagePreview([res.data.url])
  }
  return { showPerInfo }
}

// 删除订单的hook函数
export const useDelOrder = (cb: () => void) => {
  const delLoading = ref(false)
  // 删除订单
  const del = async (item: ConsultOrderItem) => {
    try {
      delLoading.value = true
      // 1. 发送请求
      await delOrder(item.id)
      // 此处不确定是通知父组件 还是返回上一页 接收的函数参数 调用函数
      cb && cb()
      // 3. 提示
      showSuccessToast('删除订单成功')
    } finally {
      delLoading.value = false
    }
  }
  return { delLoading, del }
}

// 取消问诊的hook函数
export const useCancelOrder = () => {
  const cancelLoading = ref(false)
  // 取消问诊
  const cancel = async (item: ConsultOrderItem) => {
    cancelLoading.value = true
    try {
      // 1. 发送请求
      await cancelOrder(item.id)
      // 2. 修改视图
      item.status = OrderType.ConsultCancel
      // 3. 修改右上角的文字
      item.statusValue = '已取消'
      // 4. 提示
      showSuccessToast('取消问诊成功')
      cancelLoading.value = false
    } finally {
      cancelLoading.value = false
    }
  }
  return { cancel, cancelLoading }
}

// 获取药品订单详情的hook函数
export const useMedicalDetail = (id: string) => {
  const detail = ref<OrderDetail>()
  const getDetail = async () => {
    const res = await getMedicalOrderDetail(id)
    detail.value = res.data
  }
  getDetail()
  return { detail }
}

// 倒计时hook函数
export const useCountDown = (mobile: Ref<string>, type: SendCodeType) => {
  const formRef = ref<FormInstance | null>(null)
  const time = ref(0)
  let timeId = -1
  const getCode = async () => {
    if (time.value) return // 禁止在倒计时期间再次点击
    // 校验手机号的合法性 form组件上有一个方法 validate() 括号内不传表示校验所有  传递单个name属性 表示单独校验
    await formRef.value?.validate('mobile')
    // 1. 调用接口
    await sendCodeToMobile(mobile.value, type)
    // 2. 提示
    showSuccessToast('验证码已发送')
    // 3. 倒计时
    time.value = 60
    clearInterval(timeId)
    timeId = window.setInterval(() => {
      if (time.value <= 0) return clearInterval(timeId)
      time.value--
    }, 1000)
  }
  // 组件销毁清除定时器(离开页面)
  onUnmounted(() => {
    clearInterval(timeId)
  })
  return { formRef, time, getCode }
}
